package com.fliangyun.nsa.nsamanage.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @author fuliangyun@139.com
 * @date 2023年05月30日
 * @description
 */
public class StringUtil extends StringUtils {

    /**
     * 获取UUID
     * @author fuliangyun@139.com
     * @date 2023-05-30 9:39
     * @param
     * @return String
     */
    public static String uuid(){
        return UUID.randomUUID().toString().replace("-", "").toUpperCase();
    }

    public static String getIpAddress() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip="";
        String ip0 = request.getHeader("X-Real-IP");
        String ip1 = request.getHeader("X_FORWARDED_FOR");
        String ip2 = request.getHeader("X-Forwarded-For");
        String ip3 = request.getHeader("Proxy-Client-IP");
        String ip4 = request.getHeader("WL-Proxy-Client-IP");
        String ip5 = request.getHeader("HTTP_CLIENT_IP");
        String ip6 = request.getHeader("HTTP_X_FORWARDED_FOR");
        String ip7 = request.getHeader("X-Original-Forwarded-For");
        String ip8 = request.getRemoteAddr();
        if (ip0 != null && ip0.length() != 0 && !"unknown".equalsIgnoreCase(ip0)) {
            ip += ip0+",";
        }
        if (ip1 != null && ip1.length() != 0 && !"unknown".equalsIgnoreCase(ip1)) {
            ip += ip1+",";
        }
        if (ip2 != null && ip2.length() != 0 && !"unknown".equalsIgnoreCase(ip2)) {
            ip += ip2+",";
        }
        if (ip3 != null && ip3.length() != 0 && !"unknown".equalsIgnoreCase(ip3)) {
            ip += ip3+",";
        }
        if (ip4 != null && ip4.length() != 0 && !"unknown".equalsIgnoreCase(ip4)) {
            ip += ip4+",";
        }
        if (ip5 != null && ip5.length() != 0 && !"unknown".equalsIgnoreCase(ip5)) {
            ip += ip5+",";
        }
        if (ip6 != null && ip6.length() != 0 && !"unknown".equalsIgnoreCase(ip6)) {
            ip += ip6+",";
        }
        if (ip7 != null && ip7.length() != 0 && !"unknown".equalsIgnoreCase(ip7)) {
            ip += ip7+",";
        }
        if (ip8 != null && ip8.length() != 0 && !"unknown".equalsIgnoreCase(ip8)) {
            ip += ip8+",";
        }
        if (ip.length()>1){
            return ip.substring(0, ip.length()-1);
        }
        return ip;
    }
}
